﻿using BITUI.Tile;
using SDL2;
using System;
using System.Collections.Generic;
using System.Text;

namespace BITUI.Element
{
    public enum HAlign
    {
        Left,
        Center,
        Right,
    }
    public enum VAlign
    {
        Top,
        Center,
        Bottom,
    }
    public class Label : IElement2D, IElement2D_Resizeable
    {
        public Label(Tile.Bank bank, string text)
        {
            this.bank = bank;
            this.text = text;
            this.TileWidth = 10;
            this.TileHeight = 2;
        }
        Tile.Bank bank;
        public string text;
        public byte colorReplace = 0;

        //背景 可选
        public IFillDraw backGround = null;
        public byte backGroundColorReplace = 0;
        //裁剪边缘
        public int clipborder = 0;
        public HAlign HAlign = HAlign.Center;
        public int HOffset = 0;
        public VAlign VAlign = VAlign.Center;
        public int VOffset = 0;
        public IElement2DGroup Parent
        {
            get;
            private set;
        }
        void IElement2D.SetParent(IElement2DGroup parent)
        {
            this.Parent = parent;
        }
        public int TileXLocal
        {
            get;
            set;
        }

        public int TileYLocal
        {
            get;
            set;
        }
        public int TileX
        {
            get
            {
                if (this.Parent != null)
                    return this.Parent.TileX + this.TileXLocal;
                else
                    return this.TileXLocal;
            }
        }

        public int TileY
        {
            get
            {
                if (this.Parent != null)
                    return this.Parent.TileY + this.TileYLocal;
                else
                    return this.TileYLocal;
            }
        }
        public int TileWidth
        {
            get;
            set;
        }

        public int TileHeight
        {
            get;
            set;
        }

        void IElement2D.OnDraw(IWindowRender render)
        {
            if (backGround != null)
            {
                backGround.FillDraw(render, this.TileX * 8, this.TileY * 8, this.TileWidth * 8, this.TileHeight * 8, backGroundColorReplace);
            }
            int x1 = this.TileX * 8 + clipborder;
            int y1 = this.TileY * 8 + clipborder;
            int x2 = (this.TileX + this.TileWidth) * 8 - clipborder;
            int y2 = (this.TileY + this.TileHeight) * 8 - clipborder;
            render.PushLimitTileRect(new LimitRect { xmin = x1, ymin = y1, xmax = x2, ymax = y2 });
            {
                int w = 0;
                int h = 0;
                foreach (var c in this.text)
                {
                    var tile = this.bank.GetTileByName(c.ToString());
                    if (tile != null)
                    {
                        w += tile.srcrect.w;
                        h = Math.Max(h, tile.srcrect.h);
                    }
                }
                var tx = this.TileX * 8 + HOffset;
                var ty = this.TileY * 8 + VOffset;
                if (HAlign == HAlign.Right)
                    tx += this.TileWidth * 8 - w;
                if (HAlign == HAlign.Center)
                    tx += (this.TileWidth * 8 - w) / 2;
                if (VAlign == VAlign.Bottom)
                    ty += this.TileHeight * 8 - h;
                if (VAlign == VAlign.Center)
                    ty += (this.TileHeight * 8 - h) / 2;
                render.DrawTileByNames(this.bank, this.text, tx, ty, this.colorReplace);
            }
            render.PopLimitTileRect();
        }

    }
}
